
import CryptoJS from 'crypto-js';
/*abcdefgabcdefg12*/
let key = CryptoJS.enc.Utf8.parse('123456789ABCDEFG');
let iv = CryptoJS.enc.Utf8.parse('123456789ABCDEFG');

////加密
const cryptoEncrypt = (word) => {
  var encrypted = '';
  var srcs = '';
  if (typeof word == 'string') {
    srcs = word;
  } else if (typeof word == 'object') {
    //对象格式的转成json字符串
    srcs = JSON.stringify(word);
  }
  encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  });
  return encrypted.ciphertext.toString();
};

//解密
  const cryptoDecrypt = (word) => {
  var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  var decrypt = CryptoJS.AES.decrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  });
  var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
};

export default {
    cryptoEncrypt,
    cryptoDecrypt
}